#include "vertex.cpp"

const unsigned char UN_SPHERE=0x01;
const unsigned char UN_MESH=0x02;
const unsigned char UN_BOX=0x03;

class Triangle
{
public:
	Triangle()
	{
		v[0]=0;
		v[1]=0;
		v[2]=0;
	}
	unsigned int v[3];
};

class Tangent
{
public:
	float x,y,z;
	Tangent():x(0),y(0),z(0)
	{
	}
};

class Surface
{
	friend class Entity;
	unsigned int cv;
	unsigned int ct,ci;
	int auxbuffs;
	char **auxnames;
	int *auxids;
	int *auxsystypes;
	bool fliped;
	Vertex* v;
	unsigned short* inds;
	unsigned int vertexB,texcoordB,normalsB,colorsB,tangentsB;
	unsigned int indicesB;
	unsigned int mtexcoordB[32];
	bool tclayers[32];
	float *tcoords[32];

	//unsigned int IndexesSize;
	bool glock;

	int Init();
	void UN_Sphere();
	void UN_Mesh();
	void UN_Box();

	bool use_tangents;
	Tangent *tangents;

	void _CalcTangents();
	void _CalcTangent(unsigned int tris);

	int tlayer;
	unsigned int primtype;

	int mtex;
public:
	Surface(unsigned int cntv,unsigned int cntt);
	Surface(void);
	Surface(Surface &src);
	Surface(const char *filename,float radius);
	Surface(unsigned int cntent, Entity** ppents);

	//Entity* AddEntity();

	void AddTexCoordsLayer(int layer);

	void Attach(Surface* asr,bool free);
	void AutoCenter(void);
	void ReCalcTangents(int layer);
	void ColorVertex(int i,float r,float g,float b,float a);
	unsigned int CountTris();
	unsigned int CountVerts();

	Surface *Copy();
	void Draw();

	void FitSphere(float r);
	void FitSphere(float r,float x,float y,float z);
	void FitBox(float width,float height,float depth);
	void FitBox(float x,float y,float z,float width,float height,float depth);

	bool Flip();
	void FlushVBO();

	float GetDepth();
	float GetHeight();
	int GetTrisIndex(int i,int v);
	float GetWidth();

	void LoadB3D(const char* file);
	void LoadS3M(const char* file);
	void LoadSBS(const char* file);
	void Lock();
	bool Locked();

	void MakeCross(float width,float height);
	void MakeCube(float size);
	void MakePlane(float size,unsigned int counttiles);
	void MakePulse(float lenght,float radius,float texpos,float facepos, bool bothside);
	void MakeQuad(float size,float tcscale,bool bothside);
	int MakeSphere(float size,unsigned int segments);
	void MakeTris(float size);
	int MakeVBO(void);

	int NewSize(int cntv,int cntt);

	void PositionVertex(int i,float x,float y,float z);
	void NormalVertex(int i,float x,float y,float z);

	void Rotate(float pitch, float yaw, float roll);

	void SaveSBS(const char *filename);

	void Scale(float x,float y,float z);
	void Scale(float s);
	void ScaleUV(float u,float v);
	void ScaleUV(float s);

	void SetTrisIndex(int i,int v0,int v1,int v2);
	void SetTrisIndex(int i,int* v_);
	void SetVertexX(int i);
	void SetVertexY(int i);
	void SetVertexZ(int i);

	void Translate(float x,float y,float z);

	void VertexN(int i,float nx,float ny,float nz);
	void VertexUV(int i,float u,float v);
	void VertexUV(int layer,int i,float u,float v);

	void UnLock();
	void UpdateNormals(const unsigned char method);

	void Optimize(bool stripped,int cashsize);
	~Surface();
};

typedef std::list<Surface*> Tslist; 
Tslist slist;

unsigned int OptimizeIndexes(int stripped,int cashsize,unsigned int &ci,unsigned short int *inds);